<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Request;

/**
 * 物流规则控制器
 */
class LogisticsRules extends Base
{

    protected $request;
    protected $service;
    protected $validate;
    protected $operator_id;
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\LogisticsRules();
        $this->validate = new \app\admin\validate\LogisticsRules();
        $this->operator_id = $request->operator_id;
    }

    /**
     * @describe 获取物流规则列表
     * @author: Bobo
     * @date: 2024-08-16 03:04:33
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $pageIndex  = isset($params['pageIndex'])   ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])    ? intval($params['pageSize']) : 10;
        $shippingWarehouse   = isset($params['shipping_warehouse'])   ? intval($params['shipping_warehouse']) : null;
        $toNation  = isset($params['to_nation'])   ? intval($params['to_nation']) : null;
        $terms      = isset($params['terms'])       ? intval($params['terms']) : null;
        $prescription   = isset($params['prescription'])   ? intval($params['prescription']) : null;
        $status     = isset($params['status'])      ? intval($params['status']) : null;

        $where = ['is_del' => 0];
        if ($shippingWarehouse > 0) {
            $where['shipping_warehouse'] = $shippingWarehouse;
        }
        if ($toNation > 0) {
            $where['to_nation'] = $toNation;
        }
        if ($terms > 0) {
            $where['terms'] = $terms;
        }
        if ($prescription > 0) {
            $where['prescription'] = $prescription;
        }

        if ($status > -1) {
            $where['status'] = $status;
        }


        $res = $this->service->getList($where, $pageIndex, $pageSize);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 获取物流规则详情
     * @author: Bobo
     * @date: 2024-08-16 03:07:37
     * @param [int] $id 物流规则ID @required
     * @return Renderable
     */
    public function show()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('show')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $res  = $this->service->getOneById($params['id']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 创建物流规则
     * @author: Bobo
     * @date: 2024-08-16 03:09:17
     * @param [array] $data 创建物流规则的数据 @required
     * @return Renderable
     */
    public function create()
    {

        $data = $this->request->params;
        $data['creator'] = $this->operator_id;
        if (!$this->validate->scene('create')->check($data)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }


        $result = $this->service->create($data);

        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 更新物流规则
     * @describe 更新物流规则
     * @author: Bobo
     * @date: 2024-08-16 03:11:17
     * @param [int] $id 物流规则ID @required
     * @param [array] $data 更新物流规则的数据 @required
     * @return Renderable
     */
    public function update()
    {

        $params = $this->request->params;


        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $data['updater'] = $this->operator_id;
        $data['update_time'] = time();
        $result = $this->service->update($params);

        if (!$result) {
            return $this->returnJsonData(1, $data, '数据不存在');
        }

        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除物流规则
     * @param int $id 物流规则ID
     */
    /**
     * @describe 删除物流规则
     * @author: Bobo
     * @date: 2024-08-16 03:13:56
     * @param [int] $id 物流规则ID @required
     * @return Renderable
     */
    public function delete()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('delete')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $ids =  trim($params['ids']);

        $res = $this->service->batchLogicalDelete($ids);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 
     * @author: Bobo
     * @date: 2024-08-16 19:07:39
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum()
    {
        $params = $this->request->params;
        $res = $this->service->getFieldEnum($params['field']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 查询物流费用
     * @author: Bobo
     * @date: 2024-08-19 15:21:21
     * @param [type] @required
     * @return Renderable
     */
    public function getLogisticsFee()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('getLogisticsFee')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $res = $this->service->getLogisticsFee($params);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 计算物流费用
     * @author: Bobo
     * @date: 2024-08-19 15:21:21
     * @param [type] @required
     * @return Renderable
     */
    public function calculateFee()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('calculateFee')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $res = $this->service->calculateFee($params);
        $code = $res['code'];
        $data = $res['data']?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 批量切换物流状态
     * @author: Bobo
     * @date: 2024-08-28 13:57:39
     * @param [array] ids 物流规则ID数组 @required
     * @param [int] status 状态值 @required
     * @return Renderable
     */
    public function status()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('status')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $params['updater'] = $this->operator_id;
        $params['update_time'] = time();
        $res = $this->service->status($params);

        $code = $res['code'];
        $data = $res['data'];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }


    /**
     *  物流规则 - 导入
     */
    public function import()
    {
        $params = $this->request->params;
        $params['file'] = $this->request->file('file');
        $params['creator'] = $this->operator_id;

        $res = $this->service->import($params);

        $code = $res['code'];
        $data = $res['data'];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }


    /**
     *  物流规则 - 导出
     */
    public function export()
    {
        $params = $this->request->params;
        $shippingWarehouse   = isset($params['shipping_warehouse'])   ? intval($params['shipping_warehouse']) : null;
        $toNation  = isset($params['to_nation'])   ? intval($params['to_nation']) : null;
        $terms      = isset($params['terms'])       ? intval($params['terms']) : null;
        $prescription   = isset($params['prescription'])   ? intval($params['prescription']) : null;
        $status     = isset($params['status'])      ? intval($params['status']) : null;

        $where = ['is_del' => 0];
        if ($shippingWarehouse > 0) {
            $where['shipping_warehouse'] = $shippingWarehouse;
        }
        if ($toNation > 0) {
            $where['to_nation'] = $toNation;
        }
        if ($terms > 0) {
            $where['terms'] = $terms;
        }
        if ($prescription > 0) {
            $where['prescription'] = $prescription;
        }

        if ($status > -1) {
            $where['status'] = $status;
        }

        $res = $this->service->export($where);

        $code = $res['code'];
        $data = $res['data'];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }


    /**
     * @describe 获取物流费用
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     * 2024-12-30 17 17 50
     * author: jiang
     */
    public function getLogisticsFeeByWeb(Request $request)
    {
        $checkData = $request->param();
        if (!$this->validate->scene('getLogisticsFee')->check($checkData)) {
            return json(['code' => 1, 'message' => $this->validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['shipping_warehouse'] = $request->param('shipping_warehouse', null);
        $param['terms'] = $request->param('terms', null);
        $param['to_nation'] = $request->param('to_nation', null);
        $param['prescription'] = $request->param('prescription', null);
        $param['product_list'] = $request->param('product_list', null);
        $param['currency'] = $request->param('currency', null);

        $arrData = $this->service->getLogisticsFeeByWeb($param);
        return json($arrData);
    }
}
